package com.chenwang.measure.ui.dialog;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.ProgressBar;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chenwang.measure.R;
import com.chenwang.measure.base.BaseDialogFragment;
import com.chenwang.measure.model.ObserverOnNext;
import com.chenwang.measure.model.bean.LocationBean;
import com.chenwang.measure.model.bean.ServerBean;
import com.chenwang.measure.ui.adapter.SourceAdapter;
import com.chenwang.measure.utils.MyLogger;
import com.chenwang.measure.utils.MyToast;
import com.chenwang.measure.utils.RxTime;
import com.chenwang.measure.utils.RxUtils;
import com.rxjava.rxlife.RxLife;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;
import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;

/**
 * @松 on 2019/8/25 09:02
 * 源数据
 */
public class SourceDialog extends BaseDialogFragment {
    private I mI;
    @BindView(R.id.rv)
    RecyclerView mRv;
    Unbinder unbinder;
    @BindView(R.id.spin_kit)
     ProgressBar mProgressBar;
    private List<String> mList =new ArrayList<>();
    private SourceAdapter mAdapter;


    @Override
    public int setLayoutId() {
        return R.layout.dialog_source;
    }

    @Override
    protected void initWindow(Window window) {
        setLayoutParams(window,Gravity.CENTER, WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.MATCH_PARENT,0 ,0 );
    }

    @Override
    protected void initData(View view) {
        super.initData(view);
        unbinder = ButterKnife.bind(this, view);
        initRecycler();
        Observable.create(new ObservableOnSubscribe<Boolean>() {
            @Override
            public void subscribe(ObservableEmitter<Boolean> emitter) throws Exception {
                LocationBean.getInstance().setOkMsg(false);
                LocationBean.getInstance().setMessgae(" ");
                LocationBean.getInstance().setSource(true);
                Thread.sleep(20000);
                LocationBean.getInstance().setSource(false);
                String message = LocationBean.getInstance().getMessgae();
                //截取字符串源数据
                for (int j = 0; j < 10; j++) {
                    if (message.contains("STR;")) {
                        int i = message.indexOf("STR;");
                        message = message.substring(i + 4);
                        MyLogger.i("截取" + message);
                        i = message.indexOf(";");
                        String source = message.substring(0, i);
                        if (source.length() <20) {
                            LocationBean.getInstance().setSource(source);
                        }
                    } else {
                        break;
                    }

                }

                if (LocationBean.getInstance().getSource().size()>0){
                   emitter.onNext(true);
                }else {
                    emitter.onNext(false);
                }
            }
        }).compose(RxUtils.<Boolean>io_main())
                .as(RxLife.as(this))
                .subscribe(new ObserverOnNext<Boolean>() {
            @Override
            public void onNext(Boolean aBoolean) {

                if (aBoolean){
                    mList = LocationBean.getInstance().getSource();
                    mAdapter.replaceData(mList);
                    mProgressBar.setVisibility(View.INVISIBLE);
                }else {
                   // dismiss();
                 //   mProgressBar.setVisibility(View.INVISIBLE);
                    MyToast.show("获取源数据失败 请重新获取");

                }


            }
        });


    }

    @Override
    public void onDestroy() {
        super.onDestroy();

    }

    private void initRecycler(){
          mRv.setLayoutManager(new LinearLayoutManager(getContext()));
          mAdapter = new SourceAdapter(mList);
          mAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
              @Override
              public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                           mI.success(mList.get(position));
                           dismiss();
              }
          });
          mRv.setAdapter(mAdapter);
    }



    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }


    public void setI(I i){
        mI=i;
    }
    public interface I{
        void success( String s);
    }
}
